package com.sinosoft.call.service.impl;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sinosoft.call.service.IFormatResponseJSON;

@Service
public class FormatResponseJSON implements IFormatResponseJSON{
	@Override
	public String fromatC(JSONObject callResponse) {
		String re=null;
		if(callResponse.getBooleanValue("Success")){
			JSONObject data = callResponse.getJSONObject("Data").getJSONObject("data");
			// 校验
			String status = data.getOrDefault("status","").toString();
			if("200".equals(status)){
				re = JSON.toJSONString(data.get("datas"));
			}else{
				callResponse.put("Success", false);
				callResponse.put("Message", data.getString("message"));
			}
		}
		return re;
	}

	@Override
	public String fromatP(JSONObject callResponse) {
		String re=null;
		if(callResponse.getBooleanValue("Success")){
			JSONObject data = callResponse.getJSONObject("Data");
            if(null != data && data.containsKey("code")){

                String code = data.get("code").toString();

                switch (code) {
                    case "0":
                        callResponse.put("Success", true);
                        re =JSON.toJSONString(data.get("data"));
                        break;
                    case "1":
                        callResponse.put("Success", false);
                        callResponse.put("Message","失败");
                        break;
                    case "2":
                        callResponse.put("Success", false);
                        callResponse.put("Message","系统发生异常，请稍后再试");
                        break;
                    case "404":
                    	callResponse.put("Success", false);
                    	callResponse.put("Message",data.getString("msg"));
                    default:
                        callResponse.put("Success", false);
                        callResponse.put("Message",data.getString("msg"));

                        break;
                }
            }
		}
		return re;
	}

	@Override
	public String fromatV(JSONObject callResponse) {
		String re=null;
		if(callResponse.getBooleanValue("Success")){
			JSONObject data = callResponse.getJSONObject("Data");
			if(null != data && data.containsKey("status")){

				String code = data.get("status").toString();

				switch (code) {
					case "200":
						callResponse.put("Success", true);
						//re =JSON.toJSONString(data.get("data"));
						break;
					default:
						callResponse.put("Success", false);
						callResponse.put("Message",data.getString("msg"));
						break;
				}
			}
		}
		return re;
	}

}
